// pages/patient_report/patient_report.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[
      {num:1,name:'添加就诊人',step:true,},
      {num:2,name:'完善信息',step:false,},
      {num:3,name:'选择随访',step:false,},
      {num:4,name:'完成报道',step:false,},
    ],
    archivesArr:[],
    diseaseList:[
      {checked:false,name:"糖尿病"},
      {checked:false,name:"高血压病"},
      {checked:false,name:"PICC手术"},
      {checked:false,name:"脑卒中"},
      {checked:false,name:"慢性前列腺炎"},
      {checked:false,name:"红斑狼疮"},
      {checked:false,name:"心律失常"},
      {checked:false,name:"冠心病"},
    ],
    diseaseShow:false, //显示添加疾病弹窗
    imglist:[],
    state:1,
    i:'',
    j_id:"",
    zy_time:"",
    jibin:"",
    zhuyuanhao:'',  //住院号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var zid = options.zid;
    that.setData({
      zid:zid
    })
    that.getCase();
    that.getBase();
    that.getUserjiaren();
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        that.setData({
          url: res.data,
          imgurl: res.data + "attachment/"
        })
      },
    })
  },
  getBase:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/follow.fuwu_base",
      success:function(res)
      {
        that.setData({
          base:res.data
        })
      }
    })
  },
  // 获取家人表
  getUserjiaren:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/user.alluserfamily",
      data:{
        openid:wx.getStorageSync('openid'),
      },
      success:function(res)
      {
        that.setData({
          userfamily:res.data,
          j_id:res.data[0].j_id
        })
      }
    })
  },
  // 获取随访病例表
  getCase:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/follow.follow_case",
      success:function(res)
      {
        that.setData({
          diseaseList:res.data
        })
      }
    })
  },
  // 选择就诊人
  choosepeo(e){
    var index = e.currentTarget.dataset.ind
    var that = this
    that.setData({
      i:index,
    })
  },
  // 添加就诊人
  addPeo(e){

  },
  //添加就诊人  下一步
  next(e){
    var state =e.currentTarget.dataset.state
    var that = this
    that.data.list[state].step = true
    that.setData({
      state:2,
      list:that.data.list,
    })
  },
  // 住院时间
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      zy_time:e.detail.value,
    })
  },
  // 就诊疾病
  binddisease(e){
    var that = this
    var index = e.currentTarget.dataset.index
    that.data.diseaseList[index].checked = !e.currentTarget.dataset.checked 
    this.setData({
      diseaseList:that.data.diseaseList
    })
  },
  // 添加就诊疾病
  binddisease_add(e){
    this.setData({
      diseaseShow:true
    })
  },
  bindinput(e){
    this.setData({
      diseaseName:e.detail.value
    })
  },
  queding(){
    var that = this
    var diseaseList = that.data.diseaseList
    var item = {checked:true,title:that.data.diseaseName }
    diseaseList.push(item)
    this.setData({
      diseaseShow:false,
      diseaseList:diseaseList
    })
  },
  closezhe(){
    this.setData({
      diseaseShow:false,
    })
  },
  // 病历资料 上传图片
  bindadd: function () {
    var that = this;
    wx.chooseImage({
      count: 9 - that.data.imglist.length, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有 'original', 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        //上传图片
        var successUp = 0; //成功个数
        var failUp = 0; //失败个数
        var length = res.tempFilePaths.length; //总共个数

        var i = 0; //第几个
        that.uploadDIY(res.tempFilePaths, successUp, failUp, i, length);
      },
    });
  },
  uploadDIY(filePaths, successUp, failUp, i, length) {
    var that = this;
    var uniacid = that.data.uniacid;
    var imglist = that.data.imglist;
    var uniacid = app.siteInfo.uniacid;
    wx.uploadFile({
      url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
      filePath: filePaths[i],
      name: 'upfile',
      formData: {},
      success: (resp) => {
        successUp++;
        imglist.push(resp.data);
        that.setData({
          imglist: imglist
        })
      },
      fail: (res) => {
        failUp++;
      },
      complete: () => {
        i++;
        if (i == length) {
          wx.hideLoading();
        } else { //递归调用uploadDIY函数
          this.uploadDIY(filePaths, successUp, failUp, i, length);

        }
      },
    });
  },
  //图片删除
  binddel(e){
    var t = this, ss = t.data.imglist,i=e.currentTarget.dataset.dex;
    ss.splice(i,1);
    t.setData({
      imglist:ss
    })
  },
  // 住院号
  bindzhuyuanhao(e){
    this.setData({
      zhuyuanhao:e.detail.value
    })
  },
  // 上一步
  bindprev(e){
    var state =e.currentTarget.dataset.state
    var that = this
    that.data.list[state-1].step = false
    that.setData({
      state: state-1,
      list:that.data.list,
    })
    console.log("list",that.data.list)
  },
  // 下一步
  bindnext(e){
    var state =e.currentTarget.dataset.state
    var that = this
    var zy_time = that.data.zy_time;
    that.data.list[state].step = true
    var diseaseList = that.data.diseaseList;
    var jibin = "";
    var zhuyuanhao = that.data.zhuyuanhao;
    for(var i=0;i<diseaseList.length;i++)
    {
      if(diseaseList[i].checked)
      {
        jibin += diseaseList[i].title+","
      }
    }
    jibin = jibin.substring(0,jibin.length-1);
    
    var j_id = that.data.j_id;
    if(j_id == '' && state == 1)
    {
      wx.showToast({
        title: '请选择就诊人',
      })
      return;
    }else if(state == 2 && zy_time == '')
    {
      wx.showToast({
        title: '请选择住院时间',
      })
    }else if(state == 2 && jibin == '')
    {
      wx.showToast({
        title: '请选择就诊疾病',
      })
    }else if(state ==2 && zhuyuanhao ==''){
      wx.showToast({
        title: '请输入住院号',
      })
    }else{
      that.setData({
        list:that.data.list,
        jibin:jibin
      })
      if(state == 3)
      {
        app.util.request({
          'url':"entry/wxapp/follow.changelove",
          data:{
            openid:wx.getStorageSync('openid'),
            j_id:that.data.j_id,
            zid:that.data.zid,
            zy_time:that.data.zy_time,
            zhuyuanhao:that.data.zhuyuanhao,
            jibin:that.data.jibin,
            imglist:JSON.stringify(that.data.imglist),
          },
          success:function(res)
          {
            wx.navigateTo({
              url: '/hyb_yl/huanzhe/pages/private_services/private_services?zid='+that.data.zid+'&typs=qianyue',
            })
          }
        })
      }else{
        that.setData({
          state:state+1,
        })
      }
      
    }
    
  },
  // 完成
  bindfinish(e){
    var that = this;
    setTimeout(() => {
      wx.reLaunch({
        url: '/hyb_yl/tabBar/index/index',
      })
    }, 500);
  },
})